package com.kolalo.comm;

import java.util.Map;

import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.MapContext;
import org.apache.commons.jexl3.internal.*;;

public class DyMethodUtil {

    public static Object invokeMethod(String jexlExp,Map<String,Object> map){
        JexlExpression expression = new Engine().createExpression(jexlExp);
        MapContext jc = new MapContext();
        for(String key:map.keySet()){
            jc.set(key, map.get(key));
        }
        if(null==expression.evaluate(jc)){
            return "";
        }
        return expression.evaluate(jc);
    }
    
    public static Object executeExpression(String jexlExpression, Map<String, Object> map) {
        JexlExpression expression =new Engine().createExpression(jexlExpression);
        JexlContext context = new MapContext();
        if (!map.isEmpty()) {
            map.forEach(context::set);
        }
        return expression.evaluate(context);
    }

}